/********************************************************************
filename : wolf_usart.c
discript : usart1 driver for default information output.When send one
					 byte data wait until end,when received one byte interrupt
					 event will occur.
version  : V0.0
editor   : Icy - dreamwolf
time     : 2014.2.25
statement: This file is modified by dreamwolf under LGPL,you could use 
           it free.We cann't ensure there is no error in this file,and
				   if you detect an error please contact us freely,it is so a-
					 ppreciate for your help to improve our code that could help 
					 more people to use it safty.
contact  : edreamtek@163.com
           edreamtek.taobao.com
********************************************************************/

#include "wolf_usart.h"

/* Public variables -----------------------------------------------*/
uint8_t Rx_flag;
uint8_t RxBuffer[RXBUFFERSIZE];
uint8_t NbrOfDataToRead = RXBUFFERSIZE;
__IO uint16_t RxCounter = 0; 





/********************************************************************
function:  board_usart_init(USART_InitTypeDef* USART_InitStruct)
discript: Init on board usart1.
entrance: usart parameter.
return  : none
other   : none
********************************************************************/
void board_usart_init(USART_InitTypeDef* USART_InitStruct)
{
  GPIO_InitTypeDef GPIO_InitStructure;

  /* Enable GPIO clock */
	RCC_AHBPeriphClockCmd(WOLF_COM1_TX_GPIO_CLK | WOLF_COM1_RX_GPIO_CLK, ENABLE);

  /* Enable USART clock */
  RCC_APB2PeriphClockCmd(WOLF_COM1_CLK, ENABLE); 

  /* Connect PXx to USARTx_Tx*/
  GPIO_PinAFConfig(WOLF_COM1_TX_GPIO_PORT, WOLF_COM1_TX_SOURCE, WOLF_COM1_TX_AF);

  /* Connect PXx to USARTx_Rx*/
  GPIO_PinAFConfig(WOLF_COM1_RX_GPIO_PORT, WOLF_COM1_RX_SOURCE, WOLF_COM1_RX_AF);
  
  /* Configure USART Tx as alternate function  */
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_Pin = WOLF_COM1_TX_PIN;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(WOLF_COM1_TX_GPIO_PORT, &GPIO_InitStructure);

  /* Configure USART Rx as alternate function  */
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Pin = WOLF_COM1_RX_PIN;
  GPIO_Init(WOLF_COM1_RX_GPIO_PORT, &GPIO_InitStructure);

  /* USART configuration */
  USART_Init(WOLF_COM1, USART_InitStruct);
	
	wolf_usart_NVIC_Config();				//Enable receive IT

  /* Enable USART */
  USART_Cmd(WOLF_COM1, ENABLE);
	
	USART_ClearFlag(WOLF_COM1, USART_FLAG_TC);
}


/********************************************************************
function: wolf_usart_NVIC_Config(void)
discript: Init usart receive interrupt.
entrance: none
return  : none
other   : none
********************************************************************/
void wolf_usart_NVIC_Config(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;

  /* Enable the USARTx Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = WOLF_COM1_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPriority = 0x02;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
	
	wolf_usart_RX_enable();
}

/********************************************************************
function: wolf_usart_RX_enable(void)
discript: Enable RX IT.
entrance: none
return  : none
other   : After RxBuffer[] is full or received data reach NbrOfDataToRead
					RX IT will be disable,user should to enable it manually.
********************************************************************/
void wolf_usart_RX_enable(void)
{
	USART_ITConfig(WOLF_COM1, USART_IT_RXNE, ENABLE);
	Rx_flag = FALSE;
	RxCounter = 0;
}

/********************************************************************
function: USARTx_IRQHANDLER(void)
discript: Usart1 interrupt.When received data,save them to RxBuffer[],
					and received_data_flag be TRUE.When RxBuffer[] is full or 
					received data reach NbrOfDataToRead,new data will not be re-
					ceived,and user should to enable receive interrupt manually.
entrance: none
return  : none
other   : none
********************************************************************/
void USARTx_IRQHANDLER(void)
{
  if(USART_GetITStatus(WOLF_COM1, USART_IT_RXNE) != RESET)
  {
    /* Read one byte from the receive data register */
    RxBuffer[RxCounter++] = (USART_ReceiveData(WOLF_COM1));
		Rx_flag = TRUE;
    if(RxCounter == NbrOfDataToRead | RxCounter == RXBUFFERSIZE)
    {
      /* Disable the WOLF_COM1 Receive interrupt */
      USART_ITConfig(WOLF_COM1, USART_IT_RXNE, DISABLE);
    }
  }
}


/*Retargets the C library printf function to the USART.*/
int fputc(int ch, FILE *f)
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the USART */
  USART_SendData(WOLF_COM1, (uint8_t) ch);

  /* Loop until the end of transmission */
  while (USART_GetFlagStatus(WOLF_COM1, USART_FLAG_TC) == RESET)
  {}

  return ch;
}








